package com.irdstudio.efp.esb.service.impl.sed.repay;

import com.irdstudio.efp.esb.common.client.resp.EsbRespServiceBean;
import com.irdstudio.efp.esb.common.constant.ESBClientConstance;
import com.irdstudio.efp.esb.common.constant.EsbBizEnums;
import com.irdstudio.efp.esb.service.client.ESBRequestBuilder;
import com.irdstudio.efp.esb.service.client.EsbBaseBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.constant.ESBConstant;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.basic.framework.core.util.TraceUtil;
import com.irdstudio.efp.esb.service.bo.req.sed.repay.ReqLoanRepayResultQryBean;
import com.irdstudio.efp.esb.service.bo.resp.sed.repay.RespLoanRepayResultQryBean;
import com.irdstudio.efp.esb.service.client.ESBClientFactory;
import com.irdstudio.efp.esb.service.facade.sed.repay.LoanRepayResultQryService;

/**
* 普税贷产品对税e贷产品的此部分代码做了复用
* 说明：为了保证功能的复用性、提升开发的工作效率，对该代码进行了复用
* 评估是否会影响线上功能：否
* 评估时间：2020/5/25
* 处理方案：保留复用
*/
/**
 * 需求编号：【2019D0519】（业务提的需求编号）
 * 问题编号：【xxxxxxx】（若不是因为修复问题而新增该类，则留空）
 * 开发人员：qiuyf
 * 创建日期：2019年5月7日 上午10:00:58
 * 功能描述：贷款还款结果查询接口实现
 * 30230007（贷款结果信息查询）【本服务适用于查询贷款业务相关包括放款、还款等的结果信息】
 * 01（贷款结果查询） 【贷款、还款信息的查询】
 */
@Service("loanRepayResultQryService")
public class LoanRepayResultQryServiceImpl implements LoanRepayResultQryService {

    private static Logger logger = LoggerFactory.getLogger(LoanRepayResultQryServiceImpl.class);

    @Override
    public RespLoanRepayResultQryBean loanRepayResultQry(ReqLoanRepayResultQryBean reqLoanRepayResultQryBean) throws ESBException {
        logger.info("======>调用贷款还款结果查询接口【" + reqLoanRepayResultQryBean.getTxnGlblSrlNo() + "_30230007_01】开始<======");
        RespLoanRepayResultQryBean respLoanRepayResultQryBean = null;
        if (reqLoanRepayResultQryBean.getTxnGlblSrlNo() != null) {
            try {
                EsbBaseBean esbBaseBean = EsbBaseBean.EsbBaseBeanBuilder.anEsbBaseBean().
                        withGLBLSRLNO(reqLoanRepayResultQryBean.getTxnFlowNo()).
                        withINSTID(ESBClientConstance.bdInstId).build();
                EsbRespServiceBean recvBean = ESBRequestBuilder.buildClient().
                        withEsbBaseBean(esbBaseBean)
                        .withBody(reqLoanRepayResultQryBean)
                        .withTradeNo(ESBConstant.TRADE_CODE_30230007)
                        .withScene(ESBConstant.SCENE_NO_01)
                        .create()
                        .sendAndReceive();
                if (EsbBizEnums.RetCodeEnum.SUCCESS.VALUE.equals(recvBean.getSysHead().getRetInfArry().get(0).getRetCd())) {
                    respLoanRepayResultQryBean = recvBean.getBody(RespLoanRepayResultQryBean.class);
                } else {
                    throw new ESBException(recvBean.getSysHead().getRetInfArry().get(0).getRetMsg());
                }
                logger.info("调用贷款还款结果查询接口【30230007_01】结束，返回信息：" + JSONObject.toJSONString(respLoanRepayResultQryBean));
            } catch (Exception e) {
                logger.error("调用贷款还款结果查询接口【30230007_01】出现异常：" + e.getMessage());
                throw new ESBException("调用贷款还款结果查询接口【30230007_01】出现异常：" + e.getMessage());
            } finally {
                logger.info("======>调用贷款还款结果查询接口【" + reqLoanRepayResultQryBean.getTxnGlblSrlNo() + "_30230007_01】结束<======");
            }

        }
        return respLoanRepayResultQryBean;
    }
}
